package Offer.offer2020;

/**
 * 调整数组顺序使奇数位于偶数前面
 */
public class Test14 {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        adjustArray(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }

    }

    public static void adjustArray(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }

        int start = 0;
        int end = array.length - 1;

        while (start < end) {
            while ((array[start] & 1) != 0) {//奇数
                start++;
            }
            while ((array[end] & 1) == 0) {
                end--;
            }
            if (start < end) {
                int temp = array[start];
                array[start] = array[end];
                array[end] = temp;
            }
        }
    }
}
